package com.dzx.design.creational.singletonexample;

/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年07月15日 10:03:48
 */

/**
 * 枚举式单例(推荐)
 */
public class SingletonExample5 {

    private SingletonExample5() {
    }

    public static SingletonExample5 getInstance() {
        return SingletonExample5Enum.INSTANCE.getInstance();
    }

    private enum SingletonExample5Enum {
        INSTANCE;//只有一个枚举变量，保证构造方法只调用一次


        private SingletonExample5 singletonExample5;

        //jvm保证这个方法绝对只调用一次
        SingletonExample5Enum() {
            System.out.println("我只会被调用一次");
            singletonExample5 = new SingletonExample5();
        }

        private SingletonExample5 getInstance() {
            return singletonExample5;
        }
    }
}
